package it.sources.template;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import it.sources.pojo.AiBody;
import it.sources.pojo.Messages;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import static it.sources.pojo.AiConstants.TYPE;

@Component
public class AiTemplate {
@Value("${ai.apiKey}")
    public   String API_KEY ;
    @Value("${ai.secretKey}")
    public   String SECRET_KEY ;
    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder()
            .callTimeout(5000, TimeUnit.SECONDS)
            .readTimeout(5000, TimeUnit.SECONDS)
            .writeTimeout(5000, TimeUnit.SECONDS)
            .build();
 public String chat(String text) throws IOException {
     MediaType mediaType = MediaType.parse(TYPE);
     AiBody aiBody = new AiBody();
     Messages messages = new Messages();

     messages.setContent(text);
     aiBody.setMessages(Arrays.asList(messages));
     RequestBody body = RequestBody.create(mediaType,JSON.toJSONString(aiBody));

     Request request = new Request.Builder()
             .url("https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=" + getAccessToken())
             .method("POST", body)
             .addHeader("Content-Type", TYPE)
             .build();
     Response response = HTTP_CLIENT.newCall(request).execute();
     String  bodyr=response.body().string();
     return  JSON.parseObject(bodyr).getString("result");

 }
     String getAccessToken() throws IOException {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
                + "&client_secret=" + SECRET_KEY);
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/oauth/2.0/token")
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
         String bodyS = response.body().string();
         JSONObject jsonObject= JSON.parseObject(bodyS);
         String accessToken = jsonObject.getString("access_token");
         return accessToken;
    }
//    String getAccessToken() throws IOException {
//        // 设置请求体的媒体类型为"application/x-www-form-urlencoded"
//        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
//
//        // 创建请求体，包含grant_type、client_id和client_secret
//        RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
//                + "&client_secret=" + SECRET_KEY);
//
//        // 创建请求对象
//        Request request = new Request.Builder()
//                .url("https://aip.baidubce.com/oauth/2.0/token")
//                .method("POST", body)
//                .addHeader("Content-Type", "application/x-www-form-urlencoded")
//                .build();
//
//        // 发起HTTP请求并获取响应
//        Response response = HTTP_CLIENT.newCall(request).execute();
//
//        // 从响应体中获取字符串形式的响应内容
//        String bodyS = response.body().string();
//
//        // 将响应内容解析为JSON对象
//        JSONObject jsonObject = JSON.parseObject(bodyS);
//
//        // 从JSON对象中获取access_token字段的值
//        String accessToken = jsonObject.getString("access_token");
//
//        // 返回access_token
//        return accessToken;
//    }
}
